
package com.hlkj.pay.app.merchant.ext.impl.hnapay;
/*
 * Hlpay-Plus aggregate payment system.
 * Copyright (c) 2024-2025 Hlpay Team Copyright has the right of final interpretation.
 */

import com.hlkj.framework.common.pojo.CommonResult;
import com.hlkj.framework.common.util.json.JsonUtils;
import com.hlkj.framework.extension.annotation.Extension;
import com.hlkj.pay.app.common.IUploadService;
import com.hlkj.pay.app.merchant.ext.ChannelMerchantAccessExtPt;
import com.hlkj.pay.app.merchant.ext.MerchantExtConstant;
import com.hlkj.pay.app.merchant.ext.dto.access.*;
import com.hlkj.pay.app.merchant.ext.impl.hnapay.dto.HnapayAppChannelParams;
import com.hlkj.pay.app.merchant.ext.impl.hnapay.dto.HnapayConfig;
import com.hlkj.pay.app.merchant.ext.impl.hnapay.handler.HnapayMerchantAccessConfigHandler;
import com.hlkj.pay.app.merchant.ext.impl.hnapay.handler.HnapayMerchantAccessInfoHandler;
import com.hlkj.pay.app.merchant.ext.impl.hnapay.utils.HnapayCallBackParamsParseUtil;
import com.hlkj.pay.dto.merchant.AccessChannelParamsDto;
import com.hlkj.pay.dto.merchant.MerchantAccessDto;
import com.hlkj.pay.enums.CommonEnum;
import com.hlkj.pay.enums.MerchantEnum;
import com.hlkj.pay.infrastructure.model.merchant.MerchantAccessAuthDO;
import com.hlkj.pay.infrastructure.model.merchant.MerchantAccessInfoDO;
import com.hlkj.pay.infrastructure.model.pay.ProviderChannelConfigDO;
import com.hlkj.pay.service.merchant.IMerchantAccessAuthService;
import com.hlkj.pay.util.HttpRequestUtil;
import com.hlkj.pay.vo.order.resp.MerchantProductQueryResp;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 汇付进件操作
 * 
 * @author HlpayTeam
 * @date 2024/09/26 10:25
 */
@Slf4j
@Repository
@RequiredArgsConstructor
@Extension(bizId = MerchantExtConstant.CHANNEL_HNA_PAY)
public class HnapayMerchantAccessExtPt implements ChannelMerchantAccessExtPt {

    private final IUploadService uploadService;

    private final HnapayMerchantAccessInfoHandler hnapayMerchantAccessInfoHandler;

    private final HnapayMerchantAccessConfigHandler hnapayMerchantAccessConfigHandler;

    private final IMerchantAccessAuthService merchantAccessAuthService;

    @Override
    public void channelConfigCheck(ChannelMerchantAccessCommonContext channelMerchantAccessCommonContext) {

    }
    @Override
    public void merchantAccess(ChannelMerchantAccessContext channelMerchantAccessContext) {
        log.info("新生渠道提交进件 请求参数:{}", JsonUtils.toJsonString(channelMerchantAccessContext));
        MerchantAccessInfoDO merchantAccessInfo = channelMerchantAccessContext.getMerchantAccessDto().getMerchantAccessInfo();
        //不需要进件，把request赋给submchid做唯一id
        merchantAccessInfo.setSubMchId(merchantAccessInfo.getRequestId());
        channelMerchantAccessContext.setAuditStatus(MerchantEnum.CHANNEL_CONFIG_STATUS.SUBMIT);
        channelMerchantAccessContext.setAccessStatus(MerchantEnum.ACCESS_STATUS.WAIT_SET);
    }

    @Override
    public String requestUrl(MerchantAccessDto merchantAccessInfoDO) {
        return null;
    }

    @Override
    public String channelParams(AccessChannelParamsDto accessChannelParamsDto) {
        HnapayAppChannelParams easypayAppChannelParams = new HnapayAppChannelParams();
//        merchantAccessAuthService.routerAccessAuth(accessChannelParamsDto.getSubMchId(), PayEnums.PAY_TYPE.WECHAT);
//        accessChannelParamsDto
        return JsonUtils.toJsonStringNotNull(easypayAppChannelParams);
    }

    @Override
    public void merchantAccessQuery(ChannelMerchantAccessQueryContext channelMerchantAccessQueryContext) {
        channelMerchantAccessQueryContext.setAccessStatus(MerchantEnum.ACCESS_STATUS.WAIT_SET);
        channelMerchantAccessQueryContext.setAuditStatus(MerchantEnum.CHANNEL_CONFIG_STATUS.SUBMIT);
    }

    @Override
    public void merchantSign(ChannelMerchantAccessSignContext channelMerchantAccessSignContext) {
    }

    @Override
    public void merchantSignQuery(ChannelMerchantAccessSignContext channelMerchantAccessSignContext) {
    }

    @Override
    public void merchantBusinessOpen(ChannelMerchantBusinessContext channelMerchantBusinessContext) {
        //支付宝，微信 子商户报备
        hnapayMerchantAccessConfigHandler.processSubApply(channelMerchantBusinessContext);
        // 审核中
        ChannelMerchantAccessQueryContext channelMerchantAccessQueryContext = new ChannelMerchantAccessQueryContext();
        channelMerchantAccessQueryContext.setMerchantAccessInfo(channelMerchantBusinessContext.getMerchantAccessDto().getMerchantAccessInfo());
        channelMerchantAccessQueryContext.setProviderChannelConfig(channelMerchantBusinessContext.getProviderChannelConfig());
        if (channelMerchantBusinessContext.isProcessStatus()) {
            channelMerchantBusinessContext.setAuditStatus(MerchantEnum.CHANNEL_CONFIG_STATUS.SUBMERINFO);
            channelMerchantBusinessContext.setAccessStatus(MerchantEnum.ACCESS_STATUS.APPROVE_SUCCESS);
        }

    }

    @Override
    public void wechatConfigInit(ChannelMerchantBusinessContext channelMerchantBusinessContext) {
        hnapayMerchantAccessConfigHandler.wechatConfigInit(channelMerchantBusinessContext);
        channelMerchantBusinessContext.setAuditStatus(MerchantEnum.CHANNEL_CONFIG_STATUS.WECHAT_AUTH);
    }

    @Override
    public void wechatConfigAdd(ChannelMerchantBusinessContext channelMerchantBusinessContext) {
        // 获取子商户信息进行微信参数配置
        hnapayMerchantAccessConfigHandler.processWxAuth(channelMerchantBusinessContext);
    }

    @Override
    public void wechatConfigQuery(ChannelMerchantAccessAuthQueryContext channelMerchantAccessAuthQueryContext) {
        // 文件上传完成就是微信参数自动配置
        hnapayMerchantAccessConfigHandler.processWxConfigQuery(channelMerchantAccessAuthQueryContext);
    }

    @Override
    public void wechatAuthConfig(ChannelMerchantBusinessContext channelMerchantBusinessContext) {
        // 新生 为自动认证无需处理
        channelMerchantBusinessContext.setAuditStatus(MerchantEnum.CHANNEL_CONFIG_STATUS.FINISH);
    }

    @Override
    public boolean authConfigQueryType() {
        return true;
    }

    @Override
    public void wechatAuthQuery(ChannelMerchantAccessAuthQueryContext channelMerchantAccessAuthQueryContext) {
        hnapayMerchantAccessConfigHandler.processWxAuthQuery(channelMerchantAccessAuthQueryContext);
        List<MerchantAccessAuthDO> merchantAccessAuthList = channelMerchantAccessAuthQueryContext.getMerchantAccessAuthDbList();
        MerchantAccessInfoDO merchantAccessInfo = channelMerchantAccessAuthQueryContext.getMerchantAccessInfo();
        if (!CollectionUtils.isEmpty(merchantAccessAuthList)) {
            merchantAccessAuthList.stream().forEach(merchantAccessAuthDO -> {
                merchantAccessAuthDO.setType(1);
                merchantAccessAuthDO.setSn(merchantAccessInfo.getSn());
                merchantAccessAuthDO.setAccessId(merchantAccessInfo.getId());
                if (merchantAccessAuthDO.getQrCodeArray() != null && merchantAccessAuthDO.getQrCodeArray().length > 0) {
                    CommonResult<String> commonResult = uploadService.upload(merchantAccessAuthDO.getQrCodeArray(), CommonEnum.OssEnum.IMAGE);
                    merchantAccessAuthDO.setAuthUrl(commonResult.getData());
                }
            });
        }
        channelMerchantAccessAuthQueryContext.setAuditStatus(MerchantEnum.CHANNEL_CONFIG_STATUS.ALIPAY_AUTH);
    }

    @Override
    public void alipayAuthConfig(ChannelMerchantBusinessContext channelMerchantBusinessContext) {
        // 新生 为自动认证无需处理
        channelMerchantBusinessContext.setAuditStatus(MerchantEnum.CHANNEL_CONFIG_STATUS.ALIPAY_AUTHING);
    }

    @Override
    public void alipayAuthQuery(ChannelMerchantAccessAuthQueryContext channelMerchantAccessAuthQueryContext) {
        hnapayMerchantAccessConfigHandler.processAliAuthQuery(channelMerchantAccessAuthQueryContext);
        List<MerchantAccessAuthDO> merchantAccessAuthList = channelMerchantAccessAuthQueryContext.getMerchantAccessAuthDbList();
        MerchantAccessInfoDO merchantAccessInfo = channelMerchantAccessAuthQueryContext.getMerchantAccessInfo();
        if (!CollectionUtils.isEmpty(merchantAccessAuthList)) {
            merchantAccessAuthList.stream().forEach(merchantAccessAuthDO -> {
                merchantAccessAuthDO.setType(2);
                merchantAccessAuthDO.setSn(merchantAccessInfo.getSn());
                merchantAccessAuthDO.setAccessId(merchantAccessInfo.getId());
            });
        }
        channelMerchantAccessAuthQueryContext.setAuditStatus(MerchantEnum.CHANNEL_CONFIG_STATUS.FINISH);
    }

    @Override
    public MerchantAccessCallBackDto accessCallBackParamsParse(HttpServletRequest request, ProviderChannelConfigDO providerChannelConfigDO) {
        String accessCallBody = HttpRequestUtil.requestBodyParams(request);
        return this.accessCallBackParamsParse(accessCallBody, providerChannelConfigDO);
    }

    @Override
    public CommonResult<String> accessCallbackParse(String callBackBody, List<ProviderChannelConfigDO> channelConfigDOList) {
        return null;
    }

    public MerchantAccessCallBackDto accessCallBackParamsParse(String callBackBody, ProviderChannelConfigDO providerChannelConfigDO) {
        String channelConfig = providerChannelConfigDO.getParamsConfig();
        // 配置转换
        HnapayConfig payConfig = JsonUtils.parseObject(channelConfig, HnapayConfig.class);
        return HnapayCallBackParamsParseUtil.accessCallBackParamsParse(callBackBody, payConfig);
    }

    @Override
    public MerchantBusinessCallBackDto businessCallBackParamsParse(String callBackBody, ProviderChannelConfigDO providerChannelConfigDO) {
        return null;
    }

    @Override
    public boolean verify(String callBackBody, ProviderChannelConfigDO providerChannelConfigDO) {
        String channelConfig = providerChannelConfigDO.getParamsConfig();
        // 配置转换
        HnapayConfig payConfig = JsonUtils.parseObject(channelConfig, HnapayConfig.class);
        try {
            return HnapayCallBackParamsParseUtil.verify(payConfig, callBackBody);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public String callBackResponse(CommonResult commonResult) {
        return "success";
    }

    @Override
    public void termAdd(ChannelMerchantTermContext channelMerchantTermContext) {

    }

    @Override
    public void reloadMerchantAuth(ChannelMerchantBusinessContext channelMerchantBusinessContext) {
        hnapayMerchantAccessConfigHandler.processSubApplyQuery(channelMerchantBusinessContext);
    }

    @Override
    public void channelSplit(ChannelMerchantBusinessContext channelMerchantBusinessContext) {
        // 拆分
        hnapayMerchantAccessConfigHandler.channelSplit(channelMerchantBusinessContext);
    }

    @Override
    public void productUpdate(ChannelMerchantBusinessContext channelMerchantBusinessContext) {

    }

    @Override
    public Integer queryMerAuthStatus(ChannelMerchantAccessAuthQueryContext channelMerchantAccessAuthQueryContext) {
        return null;
    }

    @Override
    public MerchantProductQueryResp productQuery(ChannelMerchantBusinessContext channelMerchantBusinessContext) {
        return null;
    }

    @Override
    public MerchantChannelSplitCallBackDto easySplitCallBackParamsParse(HttpServletRequest request, ProviderChannelConfigDO providerChannelConfigDO) {
        return null;
    }

}
